{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tushare as ts\n",
    "from datetime import date\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Getting data:]#######################################"
     ]
    }
   ],
   "source": [
    "hq = ts.get_today_all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "hq = hq[['code', 'name', 'changepercent', 'trade', 'volume', 'turnoverratio']].sort_values('changepercent', ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "adf = hq.head(15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "template = \"hq.docx\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'today', 'sz', 'name', 'trade', 'volume', 'szp', 'turnoverratio', 'sh', 'cyb', 'code', 'shp', 'cybp', 'changepercent'}\n"
     ]
    }
   ],
   "source": [
    "document = ts.MailMerge(template)\n",
    "#查看有哪些域属性\n",
    "print(document.get_merge_fields())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "heads = {'today':'{:%Y-%m-%d}'.format(date.today()),\n",
    "    'sh': '3253.43',\n",
    "    'shp': '0.06%',\n",
    "    'sz': '10443.73',\n",
    "    'szp': '0.11%',\n",
    "    'cyb': '1938.44',\n",
    "    'cybp': '0.65%'}\n",
    "\n",
    "document.merge(**heads)\n",
    "document.merge_rows('code', json.loads(adf.to_json(orient='records',force_ascii=False)))\n",
    "document.write('d:\\\\hqs.docx')"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
